package com.llsydn.hello;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * @author lilinshen
 * @title 一个activity启动另一个activity
 * @description 一般启动、带回调的启动
 * @date 2018/8/30 11:10
 */
public class FirstActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_first_message;
    private Button btn_first_start1;
    private Button btn_first_start2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        et_first_message = findViewById(R.id.et_first_message);
        btn_first_start1 = findViewById(R.id.btn_first_start1);
        btn_first_start2 = findViewById(R.id.btn_first_start2);

        btn_first_start1.setOnClickListener(this);
        btn_first_start2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == btn_first_start1) {
            // 创建Intent对象(显示意图)
            Intent intent = new Intent(this, SecondActivity.class);
            String message = et_first_message.getText().toString();
            // 携带额外的数据
            intent.putExtra("Message", message);
            // 启动Activity
            startActivity(intent);
        } else if (v == btn_first_start2) {
            // 创建Intent对象(显示意图)
            Intent intent = new Intent(this, SecondActivity.class);
            String message = et_first_message.getText().toString();
            // 携带额外的数据
            intent.putExtra("Message", message);
            // 带回调的启动
            int requestCode = 2;
            startActivityForResult(intent, requestCode);
        }
    }

    /**
     * 带回调的返回
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // 判断code
        if (requestCode == 2 && resultCode == 3) {
            // 从data中取数据
            String result = data.getStringExtra("Result");
            // 显示
            et_first_message.setText(result);
        }
    }

}
